package com.test.prolenders.dialog_prolenders;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.test.prolenders.R;
import com.test.prolenders.bean_prolenders.config_prolenders.AppShowCustomize_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;


public class CustomerServiceDialog_prolenders extends Dialog {

    private LinearLayout groupLayout;

    private final String page = "DIALOG_CUSTOMER_SERVICE";

    public CustomerServiceDialog_prolenders(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_customer_service_prolenders);

        getWindow().setBackgroundDrawableResource(R.color.transparent_prolenders);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        init();
    }

    @SuppressLint("SetTextI18n")
    private void init(){

        findViewById(R.id.dcs_ib_close).setOnClickListener(v -> dismiss());

        String hint = AppShowCustomize_prolenders.getInstance().getCustomerServiceHint();
        String[] mobiles = AppShowCustomize_prolenders.getInstance().getMobiles();
        String[] emails = AppShowCustomize_prolenders.getInstance().getEmails();

        TextView descTv = findViewById(R.id.dcs_tv_desc);
        if (!TextUtils.isEmpty(hint)) descTv.setText(hint);

        groupLayout = findViewById(R.id.dcs_layout_group);

        for (int i=0;i<mobiles.length;i++){
            String mobile = mobiles[i];
            addMobile(mobile, (mobiles.length<=1 ? -1:i+1), !(i==mobiles.length-1 && emails.length==0));
        }

        for (int i=0;i<emails.length;i++){
            String email = emails[i];
            addEmail(email, (emails.length<=1 ? -1:i+1), !(i==emails.length-1));
        }

    }

    @SuppressLint("SetTextI18n")
    private void addMobile(String mobile, int num, boolean showLine){
        if (TextUtils.isEmpty(mobile)) return;

        View mobileItem = View.inflate(getContext(),R.layout.item_customer_service_prolenders,null);

        TextView titleTv = mobileItem.findViewById(R.id.ics_tv_title);
        TextView textTv = mobileItem.findViewById(R.id.ics_tv_text);
        TextView btnTv = mobileItem.findViewById(R.id.ics_tv_btn);
        View lineView = mobileItem.findViewById(R.id.ics_view_line);

        titleTv.setText(getContext().getString(R.string.customer_service_hotline_title_prolenders) + (num>0 ? " "+num:""));
        textTv.setText(mobile);
        btnTv.setText(getContext().getString(R.string.dial_prolenders));
        lineView.setVisibility(showLine ? View.VISIBLE:View.GONE);

        btnTv.setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"PHONE_CALL","");
            Intent dialIntent =  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile.trim()));//跳转到拨号界面，同时传递电话号码
            getContext().startActivity(dialIntent);
        });

        groupLayout.addView(mobileItem);
    }

    @SuppressLint("SetTextI18n")
    private void addEmail(String email, int num, boolean showLine){
        if (TextUtils.isEmpty(email)) return;

        View emailItem = View.inflate(getContext(),R.layout.item_customer_service_prolenders,null);

        TextView titleTv = emailItem.findViewById(R.id.ics_tv_title);
        TextView textTv = emailItem.findViewById(R.id.ics_tv_text);
        TextView btnTv = emailItem.findViewById(R.id.ics_tv_btn);
        View lineView = emailItem.findViewById(R.id.ics_view_line);

        titleTv.setText(getContext().getString(R.string.customer_service_email_title_prolenders) + (num>0 ? " "+num:""));
        textTv.setText(email);
        btnTv.setText(getContext().getString(R.string.copy_prolenders));
        lineView.setVisibility(showLine ? View.VISIBLE:View.GONE);

        btnTv.setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"EMAIL_COPY","");
            ((ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText("Label", email.trim()));
            Toast.makeText(getContext(), getContext().getString(R.string.copy_success_prolenders), Toast.LENGTH_SHORT).show();
        });

        groupLayout.addView(emailItem);
    }

    @Override
    public void show() {
        super.show();
        HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"START","");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"END","");
    }
}
